<?php

/**
 *      出库记录控制器
 *      [X-Mis] (C)2007-2099  
 *      This is NOT a freeware, use is subject to license terms
 *      http://www.seemine.cn
 *      tel:(028)82685343
 *      qq:16129825
 */

namespace Home\Controller;
use Think\Controller;

class ProoutController extends CommonController{

   public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }
	
   function _filter(&$map) {
	   if(!in_array(session('uid'),C('ADMINISTRATOR'))){
		$map['uid'] = array('EQ', session("uid"));
	   }

	}
	
   public function _befor_index(){ 
   
   }
  
  
  public function _befor_add(){
	  $attid=time();
	  $this->assign('attid',$attid);

    
  }
	
 //   public function _after_add($id){
	// $upda['jpzongjia']=I("jpjiage")*I("shuliang");
	// M("proout")->data($upda)->save();
	
    
 //   }


  public function _befor_insert($data){
  	$data['zongjiajin']=I("jpjiage")*I("shuliang");
  	$data['addm']=date("Y-m",time());
	 M("pro")->where('id='.I("jpid"))->setInc('chuku',I("shuliang"));
	 M("pro")->where('id='.I("jpid"))->setDec('kucun',I("shuliang"));
	 return $data;
  }
  
  public function _befor_edit(){
     $model = D($this->dbname);
	 $info = $model->find(I('get.id'));
	 $attid=$info['attid'];
	 $this->assign('attid',$attid);
  }
   
  public function _befor_update($data){

  }
  
 //    public function _after_edit($id){
	// $upda['jpzongjia']=I("jpjiage")*I("shuliang");
	//  M("proout")->data($upda)->save();
 //   }


   public function _befor_del($id){
	  
   }

   public function outxls() {
		$model = D($this->dbname);
		$map = $this->_search();
		if (method_exists($this, '_filter')) {
			$this->_filter($map);
		}
		$list = $model->where($map)->field('id,jpname,jpjiage,jpguige,shuliang,title,hytitle,hyxming,hyphone,addtime')->select();
	    $headArr=array('ID','产品名称','产品价格','规格型号','出库数量','说明','关联客户','联系人','电话','入库时间');
	    $filename='销售记录';
		$this->xlsout($filename,$headArr,$list);
	}
	
	public function fenxi(){
	 $this->display();
	}
	public function add() {
 	
		if(IS_POST){
		  $model = D($this->dbname);
		  $data=I('post.');
		  $data['jpzongjia']=I("jpjiage")*I("shuliang");
		  if (false === $data = $model->create()) {
			   $this->mtReturn(300,'失败，请检查值是否已经存在',$_REQUEST['navTabId'],true);  
            }

          if (method_exists($this, '_befor_insert')) {
                $data = $this->_befor_insert($data);
            }	  
          if($model->add($data)){
			if (method_exists($this, '_after_add')) {
			  $id = $model->getLastInsID();
			  $this->_after_add($id);
		    }
			$id = $model->getLastInsID();
			$this->mtReturn(200,"新增成功".$id,$_REQUEST['navTabId'],true);  
		  }
	      
		}
		if (method_exists($this, '_befor_add')) {
			$this->_befor_add();
		}
		$this->display();
	}


	public function edit() {
	  $model = D($this->dbname);
	  if(IS_POST){
		   $data=I('post.');
		    $data['jpzongjia']=I("jpjiage")*I("shuliang");
		   if (false === $data = $model->create()) {
			   $this->mtReturn(300,'失败，请检查值是否已经存在',$_REQUEST['navTabId'],true);  
            }
           if (method_exists($this, '_befor_update')) {
                $data = $this->_befor_update($data);
            }		
          if($model->save($data)){
			if (method_exists($this, '_after_edit')) {
			  $id = $data['id'];
			  $this->_after_edit($id);

			  } 
		  }	
          $id = $data['id'];
		  $this->mtReturn(200,"编辑成功".$id,$_REQUEST['navTabId'],true); 		  
	   }
	     if (method_exists($this, '_befor_edit')) {
			$this->_befor_edit();
		 }
		$id = $_REQUEST [$model->getPk()];
		$vo = $model->getById($id);
		$this->assign('id',$id);
		$this->assign('Rs', $vo);
		$this->display();
	}





}

